<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            clearable
            v-model="goodsInfo.query"
            @clear="searchGoods"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="searchGoods"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="16">
          <el-button type="primary" @click="$router.push('/goods/add')"
            >添加商品</el-button
          >
        </el-col>
      </el-row>
      <!--
        :show-overflow-tooltip="true"  列属性的文本溢出隐藏
      -->
      <el-table :data="goodsList" border stripe v-loading="isLoading">
        <el-table-column type="index"></el-table-column>
        <el-table-column
          label="商品名称"
          prop="goods_name"
          min-width="300px"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          label="商品价格(元)"
          prop="goods_price"
          min-width="90px"
        ></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight"></el-table-column>
        <el-table-column label="商品数量" prop="goods_number"></el-table-column>
        <el-table-column label="创建时间" min-width="180px">
          <template slot-scope="scope">
            {{ scope.row.add_time | fomateDate }}
          </template>
        </el-table-column>
        <el-table-column label="操作" prop="goods_name" min-width="120px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="editGoods(scope.row.goods_id)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="delGoods(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="goodsInfo.pagenum"
        :page-sizes="[10, 15, 20, 25]"
        :page-size="goodsInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
import * as goods from '../../api/goods'
export default {
  data () {
    return {
      goodsInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      goodsList: [],
      total: 0,
      isLoading: true
    }
  },
  created () {
    this.getGoodsList()
  },
  mounted () {
    this.$nextTick(() => {
      this.isLoading = false
    })
  },
  methods: {
    async getGoodsList () {
      const { data: res } = await goods.getGoodsList(this.goodsInfo)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.goodsList = res.data.goods
      this.total = res.data.total
      console.log(res)
    },
    handleSizeChange (newSize) {
      this.goodsInfo.pagesize = newSize
      this.getGoodsList()
    },
    handleCurrentChange (newPage) {
      this.goodsInfo.pagenum = newPage
      this.getGoodsList()
    },
    searchGoods () {
      this.getGoodsList()
    },
    async delGoods (id) {
      const result = await this.$confirm(
        '此操作将永久删除该商品, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      if (result === 'cancel') return this.$message.info('取消删除')
      const { data: res } = await goods.delGoods(id)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.$message.success(res.meta.msg)
      this.getGoodsList()
    },
    async editGoods (id) {
      const { data: res } = await goods.editGoodsById(id)
      console.log(res)
    }
  }
}
</script>

<style lang="less" scoped></style>
